export const getQueryVariable = function (variable) {
	let query = window.location.search.substring(1);
	let vars = query.split("&");
	for (let i = 0; i < vars.length; i++) {
		let pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}


export const getStorage = function (name) {
	if (!name) return '';
	return localStorage.getItem(name);
}

export const setStorage = function (name, value) {
	if (!name || !value) return '';
	localStorage.setItem(name, value);
}

export const removeStorage = function (name) {
	if (!name) return false;
	localStorage.removeItem(name);
}

export const clearStorage = function (name, value) {
	localStorage.clear();
}

export const getCookie = function (name) {
	let v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
	return v ? decodeURIComponent(v[2]) : null;
}

export const setCookie = function (name, value, days) {
	let d = new Date;
	d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
	let decodeValue = encodeURIComponent(value);
	document.cookie = name + "=" + decodeValue + ";path=/;expires=" + d.toGMTString();
}

export const deleteCookie = function (name) {
	let exp = new Date();
	exp.setDate(exp.getDate() - 1);
	document.cookie = name + "=;expires=" + exp;
}

export const priceSwitch = function (x) {
	//强制保留两位小数
	var f = parseFloat(x);
	if (isNaN(f)) return false;
	var f = Math.round(x * 100) / 100;
	var s = f.toString();
	var rs = s.indexOf('.');
	if (rs < 0) {
		rs = s.length;
		s += '.';
	}
	while (s.length < (rs + 1) + 2) {
		s += '0';
	}
	//每三位用一个逗号隔开
	var leftNum = s.split(".")[0];
	var rightNum = "." + s.split(".")[1];
	var result;
	//定义数组记录截取后的价格
	var resultArray = new Array();
	if (leftNum.length > 3) {
		var i = true;
		while (i) {
			resultArray.push(leftNum.slice(-3));
			leftNum = leftNum.slice(0, leftNum.length - 3);
			if (leftNum.length < 4) {
				i = false;
			}
		}
		//由于从后向前截取，所以从最后一个开始遍历并存到一个新的数组，顺序调换
		var sortArray = new Array();
		for (var i = resultArray.length - 1; i >= 0; i--) {
			sortArray.push(resultArray[i]);
		}
		result = leftNum + "," + sortArray.join(",") + rightNum;
	} else {
		result = s;
	}
	return result;
}

export const formatterDate = function (now) {
	if (!now) {
		var now = new Date();
	}
	let year = now.getFullYear();
	let month = now.getMonth() + 1 < 10 ? ('0' + (now.getMonth() + 1)) : (now.getMonth() + 1);
	let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
	return {year, month, day}
}

export const formatterDateString = function (now) {
	if (!now) {
		var now = new Date();
	}
	let year = now.getFullYear();
	let month = now.getMonth() + 1 < 10 ? ('0' + (now.getMonth() + 1)) : (now.getMonth() + 1);
	let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
	return `${year}-${month}-${day}`
}

export const formatterTime = function (now) {
	if (!now) {
		var now = new Date();
	}
	let year = now.getFullYear();
	let month = now.getMonth() + 1 < 10 ? ('0' + (now.getMonth() + 1)) : (now.getMonth() + 1);
	let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
	let hours = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
	let minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
	return `${year}-${month}-${day} ${hours}:${minutes}`
}

export function hasClass(ele, cls) {
	return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
}

export function addClass(ele, cls) {
	if (!hasClass(ele, cls)) ele.className += ' ' + cls
}

export function removeClass(ele, cls) {
	if (hasClass(ele, cls)) {
		const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
		ele.className = ele.className.replace(reg, ' ')
	}
}

// 递归方式实现深拷贝
export function DeepCopy(obj) {
	const return_obj = {};
	for (let key in obj) {
		if (Object.prototype.toString.call(obj[key]) === '[Object Object]') {
			return_obj[key] = DeepCopy(obj[key]);
		} else {
			return_obj[key] = {...obj[key]};  // es6语法糖，处理当前层次赋值为深拷贝
		}
	}
	return return_obj;
}

export function positiveIntegerReg(num) { // 正整数 正则
	return /^[0-9]\d*$/.test(num);
}

export function positiveIntegerAndDecimalReg(num) { // (正整数 或 正小数-小数点后两位) 正则
	return /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*))|0$/.test(num);
}

export function userNameReg(name) {  // 姓名中文正则 包含少数民族
	return /^[\u4E00-\u9FA5]+$/.test(name)
}

export function containChinese(name) {  // 包含至少一个中文字
	return /.*[\u4e00-\u9fa5]+.*$/.test(name)
}


// 节流
export function throttle(func, wait) {
	let timeout, ctx, args, result;
	let previous = 0;
	
	let after = function () {
		previous = +new Date();
		timeout = null;
		func.apply(ctx, args);
	}
	return function (...args) {
		let now = +new Date();
		// 下次触发func剩余的时间
		let remaining = wait - (now - previous);
		ctx = this;
		// 如果没有剩余的时间了或者你改了系统时间
		if (remaining <= 0 || remaining > wait) {
			if (timeout) {
				clearTimeout(timeout);
				timeout = null;
			}
			previous = now;
			func.apply(ctx, args);
		} else if (!timeout) {
			timeout = setTimeout(after, remaining);
		}
	};
}
